package m5_topic;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.Scanner;

public class Consumer {
    public static void main(String[] args) throws Exception{
        ConnectionFactory f=new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Channel c =f.newConnection().createChannel();

        //1定义随机队列  2定义交换机  3用绑定键绑定
        String queue = c.queueDeclare().getQueue();//由rabbitmq服务器自动命名，非持久，独占，自动删除
        c.exchangeDeclare("topic_logs", BuiltinExchangeType.TOPIC);
        System.out.println("输入绑定建,用空格隔开： ");
        String s = new Scanner(System.in).nextLine();
        String[] a = s.split("\\s+");
        for(String key:a){
            c.queueBind(queue, "topic_logs", key);
        }
        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                String msg=new String(message.getBody());
                String key=message.getEnvelope().getRoutingKey();
                System.out.println("收到："+msg+"-"+key);

            }
        };

        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String s) throws IOException {

            }
        };

        c.basicConsume(queue, true, deliverCallback, cancelCallback);


    }
}
